package org;

import java.util.*;

/**
 * 重庆市内 电信和联通的IP库
 * 按照特定策略生成
 */
public class IPPackage {
    //重庆市 联通
    // 58.17.128.0 - 58.17.136.1
    //58.17.136.35-58.17.136.77
    //58.17.136.79 - 58.17.137.5
    //58.17.138.0 - 58.17.138.17
    //重庆 电信
    //222.182.39.60 - 222.182.40.181
    //222.182.71.0 - 222.182.71.255
    //125.86.184.0 - 125.86.255.255
    //125.84.50.0 - 125.84.52.255
    //125.84.234.0 - 125.84.234.255
    //125.82.120.0 - 125.82.120.255 南岸区
    //61.186.236.78 - 61.186.236.114 南岸区

    private static HashMap<Integer,Stack<String>> lts = null;
    private static HashMap<Integer,Stack<String>> dxs = null;

    static{
        if(lts==null) {
            lts = new HashMap();
            dxs = new HashMap<Integer, Stack<String>>();

            //联通第一批次
            Stack<String> lts1 = new Stack<String>();
            String ip1 = "58.17.";
            for (int i = 128; i < 136; i++) {
                for (int j = 1; j < 254; j++) {
                    String ipp1 = ip1 + i + "." + j;
                    lts1.push(ipp1);
                }
            }

            Collections.shuffle(lts1);

            lts.put(0, lts1);

            //联通第二批次
            Stack<String> lts2 = new Stack<String>();
            String ip2 = "58.17.136.";
            for (int i = 35; i < 77; i++) {
                String ipp2 = ip2 + i;
                lts2.push(ipp2);
            }

            Collections.shuffle(lts2);
            lts.put(1, lts2);

            //联通第三批次
            Stack<String> lts3 = new Stack<String>();
            String ip3 = "58.17.";
            for (int i = 136; i < 137; i++) {
                for (int j = 79; j < 254; j++) {
                    String ipp3 = ip3 + i + "." + j;
                    lts3.push(ipp3);
                }
            }

            Collections.shuffle(lts3);
            lts.put(2, lts3);

            //联通第四批次
            Stack<String> lts4 = new Stack<String>();
            String ip4 = "58.17.138.";
            for (int i = 0; i < 17; i++) {
                String ipp4 = ip4 + i;
                lts4.push(ipp4);
            }

            Collections.shuffle(lts4);
            lts.put(3, lts4);


            //重庆 电信
            //222.182.39.60 - 222.182.40.181
            //222.182.71.0 - 222.182.71.255
            //125.86.184.0 - 125.86.255.255
            //125.84.50.0 - 125.84.52.255
            //125.84.234.0 - 125.84.234.255
            //125.82.120.0 - 125.82.120.255 南岸区
            //61.186.236.78 - 61.186.236.114 南岸区

            //电信第一批次
            Stack<String> dxs1 = new Stack<String>();
            String dip1 = "222.182.";
            for (int i = 39; i < 41; i++) {
                for (int j = 60; j < 181; j++) {
                    String ipp1 = dip1 + i + "." + j;
                    dxs1.push(ipp1);
                }
            }

            Collections.shuffle(dxs1);
            dxs.put(0,dxs1);

            //电信第二批次
            Stack<String> dxs2 = new Stack<String>();
            String dip2 = "222.182.71.";
            for (int i = 0; i < 254; i++) {
                String ipp2 = dip2+i;
                dxs2.push(ipp2);
            }

            Collections.shuffle(dxs2);
            dxs.put(1,dxs2);

            //电信第三批次
            Stack<String> dxs3 = new Stack<String>();
            String dip3 = "125.86.";
            for (int i = 184; i < 255; i++) {
                for (int j = 0; j < 254; j++) {
                    String ipp = dip3 + i + "." + j;
                    dxs3.push(ipp);
                }
            }
            Collections.shuffle(dxs3);
            dxs.put(2,dxs3);

            //电信第四批次
            Stack<String> dxs4 = new Stack<String>();
            String dip4 = "125.84.";
            for (int i = 50; i < 53; i++) {
                for (int j = 0; j < 254; j++) {
                    String ipp = dip4 + i + "." + j;
                    dxs4.push(ipp);
                }
            }
            Collections.shuffle(dxs4);
            dxs.put(3,dxs4);

            //电信第五批次
            Stack<String> dxs5 = new Stack<String>();
            String dip5 = "125.84.234.";
            for (int i = 0; i < 254; i++) {
               String ipp = dip5 + i;
               dxs5.push(ipp);
            }
            Collections.shuffle(dxs5);
            dxs.put(4,dxs5);

            //电信第6批次
            Stack<String> dxs6 = new Stack<String>();
            String dip6 = "125.82.120.";
            for (int i = 0; i < 254; i++) {
                String ipp = dip6 + i;
                dxs6.push(ipp);
            }
            Collections.shuffle(dxs6);
            dxs.put(5,dxs6);

            //电信第7批次
            Stack<String> dxs7 = new Stack<String>();
            String dip7 = "61.186.236.";
            for (int i = 78; i < 114; i++) {
                String ipp = dip7 + i;
                dxs7.push(ipp);
            }
            Collections.shuffle(dxs7);
            dxs.put(6,dxs7);

        }
    }

    /**
     * 生成策略：
     * 1、比例：按照联通：电信=1：5的比例进行生成
     */
    public static String select(){
        Random random = new Random();
        int factor = random.nextInt(5);
        if(factor==1){
            //如过factor等于1，则生成联通的IP
            //随机选取联通库里的一个序列
            int length = lts.size();
            int rsize = random.nextInt(length);
            boolean flag = true;
            String ip = null;
            while(flag) {
                ip = lts.get(rsize).pop();
                if(!JedisServ.exists(ip)){
                    flag = false;
                }
            }
            return ip;
        }else{
            //生成电信的IP
            int length = dxs.size();
            int rsize = random.nextInt(length);
            boolean flag = true;
            String ip = null;
            while(flag) {
                ip = dxs.get(rsize).pop();
                if(!JedisServ.exists(ip)){
                    flag = false;
                }
            }
            return ip;
        }
    }

}
